class DashController < ApplicationController
     def landing_page
     #First page we should load is the login
     redirect_to login_url
     end

     def analytics    #The analytics side is handled through jQuery.
          @stockClicked = params["symbol"] if params["symbol"]
     end

     def dashboard #

          @quickWatch = getWatchedStocks(@current_user.id)

          self.friendrequest if request.post? and params['friendname']
          self.changeQuickwatch if request.post? and params['symbol']
          self.confirmrequest if request.post? and params['accept']

          @friendRequests = []
          
          for u in User.find(Friend.where(friend_id: @current_user.id, confirmed: false).map(&:user_id)) do
               @friendRequests << {name: "#{u.username} (#{u.firstname})", id: u.id.to_s}
          end

          @username = @current_user.username

          #@friends should come in sorted by last active time then by name.
          @friends = []
          
          User.find(Friend.where(user_id: @current_user, confirmed: true).map(&:friend_id)).each do |u|
               @friends << {'name'=>u.username, 'active'=>u.active}
          end
          
          @friends.sort! do |a,b|
               b["active"]<=>a["active"]
          end

          @motd = "We're putting together a sweet game for you guys, stay tuned!"
     end
  



     def friendrequest
          unless params['friendname'] then
               @errors << params.to_s + "in friendrequest"
               return
          end

          name = sanitize params['friendname']
          @errors << "Don't put html in there!" if name != params['friendname']
          user = User.find_by_username(name)
          
          if user == nil
               @errors << "That user doesn't exist" 
               return
          end

          f = Friend.find_by(user_id: @current_user, friend_id: user.id)
          
          if f
               if f.confirmed
                    @errors << "You are already friends with #{user.username}"
               elsif
                    @errors << "You already sent a friend request to #{user.username}"
               end
          end

          return if @errors.length > 0

          f = Friend.create(user_id: @current_user.id, friend_id: user.id, confirmed: false)
          
          if f.errors.size > 0
               @errors << f.errors.full_messages
          else
               @message = "Friend request sent to #{name}!"
          end
     end




     def confirmrequest
          unless params['friend_id'] then
               @errors << params.to_s + "in confirmrequest"
               return
          end

          fid = params['friend_id'].to_i
          accept = params['accept'] == 'true'

          if fid <= 0 or (!accept and params['accept'] != 'false')
               @errors << "invalid parameters"
          end

          friendship = Friend.find_by(user_id: User.find(fid), friend_id: @current_user.id)
          
          if friendship == nil or friendship.friend_id != @current_user.id 
               @errors << "That person never asked to be friends" 
          end
          
          if friendship.confirmed
               @errors << "You are already friends with #{User.find(friendship.user_id).username}" 
          end

          return if @errors.length > 0

          if accept
               friendship.confirmed = true
               friendship.save
               Friend.create(user_id: @current_user.id, friend_id: friendship.user_id, confirmed: true)
               @message = "You made a friend!"
          else #rejection!
               friendship.delete
               @message = "Request Deleted"
          end
     end
end

